package com.itcaius.myandroidhomework.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.itcaius.myandroidhomework.MyApplication;
import com.itcaius.myandroidhomework.R;
import com.itcaius.myandroidhomework.data.callback.DatabaseCallback;
import com.itcaius.myandroidhomework.data.entity.UserEntity;
import com.itcaius.myandroidhomework.data.repo.UserRepository;

import org.mindrot.jbcrypt.BCrypt;


public class LoginActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private Button btn_login;
    private UserRepository userRepo;
    private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
    //初始化方法
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);

        userRepo = MyApplication.getUserRepo();

        btn_login.setOnClickListener(v->{
            String username = et_username.getText().toString();
            String password = et_password.getText().toString();

            userRepo.getUser(username, new DatabaseCallback<UserEntity>() {
                @Override
                public void onDataLoaded(UserEntity data) {
                    mainThreadHandler.post(()->{
                        if (data == null) {
                            //用户名对应用户不存在，不能登录
                            Toast.makeText(LoginActivity.this, "用户名不存在", Toast.LENGTH_LONG).show();
                            return;
                        }else{
//                            Log.i("user",data.toString());
//                            Log.i("Hashed Password", data.getPassword());
//                            Log.i("Password", password);
//                            boolean checkpw = BCrypt.checkpw(password, data.getPassword());
//                            Log.i("checkpw", String.valueOf(checkpw));
                            //同时密码正确
                            if (data.checkPassword(password)) {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(LoginActivity.this, BookListActivity.class);
                                startActivity(intent);
                            }else{
                                Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_LONG).show();
                            }
                        }

                    });
                }

                @Override
                public void onError(Throwable error) {
                    mainThreadHandler.post(()-> {
                        Toast.makeText(LoginActivity.this, "用户登录失败", Toast.LENGTH_LONG).show();
                    });
                }
            });
        });
    }
}
